#!/usr/bin/env python
# encoding: utf-8
"""
@Author: Beam
@Mail:506556658@qq.com
@file: 协程gevent-爬网页并发.py
@time: 2017/5/10 9:01
"""

from gevent import monkey
monkey.patch_all()   #把当前程序的所有有可能进行IO操作的都标记起来，让gevent可以识别

import gevent
from  urllib.request import urlopen
import time

def f(url):
    print('GET: %s' % url)
    resp = urlopen(url)
    data = resp.read()
    print('%d bytes received from %s.' % (len(data), url))

#串行执行
start_time = time.time()
urls = ['https://www.python.org/','https://www.yahoo.com/','https://github.com/']
for i in urls:
    f(i)
print("串行执行总时间：",time.time()-start_time)

#异步gevent执行
async_start_time = time.time()
gevent.joinall([
    gevent.spawn(f, 'https://www.python.org/'),
    gevent.spawn(f, 'https://www.yahoo.com/'),
    gevent.spawn(f, 'https://github.com/'),
])
print("异步执行总时间：",time.time()-async_start_time)